using System;
using System.Text;

namespace FeedFly
{
    /// <summary>
    /// Static helper class for managing configuration settings. 
    /// </summary>
    public static class Settings
    {
        private const string downloadIntervalSettingName = "DownloadInterval";
        private static int downloadInterval = -1;
        private const string maxItemCountSettingName = "MaxItemCount";
        private static int maxItemCount = -1;
        private const string defaultItemLimitSettingName = "DefaultItemLimit";
        private static string defaultItemLimit;
        private const string fullScreenViewSettingName = "FullScreenView";
        private static string fullScreenView;

        private static string Get(string setting)
        {
            // connect to database and retrieve setting
            string value = null;

            StringBuilder sql = new StringBuilder("SELECT Value FROM Settings WHERE Name = '", 100);

            value = Data.ExecuteScalar(sql.Append(setting).Append("'").ToString()).ToString();

            return value;
        }

        private static void Set(string setting, string value)
        {
            StringBuilder sql = new StringBuilder("UPDATE Settings SET Value ='", 100);
            sql.Append(value).Append("' WHERE Name = '").Append(setting).Append("'");

            Data.ExecuteNonQuery(sql.ToString());
        }

        // Item limit is returned as a string since it will be used 
        // primarily for populating the text box control on the new feed screen
        public static string DefaultItemLimit
        {
            get
            {
                if (defaultItemLimit == null)
                    defaultItemLimit = Get(defaultItemLimitSettingName);
                return defaultItemLimit;
            }
            set
            {
                if (defaultItemLimit == null || !defaultItemLimit.Equals(value))
                {
                    // update cached setting
                    defaultItemLimit = value;
                    // update in database
                    Set(defaultItemLimitSettingName, value);
                }
            }
        }

        public static bool FullScreenView
        {
            get
            {
                if (fullScreenView == null)
                    fullScreenView = Get(fullScreenViewSettingName);

                return "1".Equals(fullScreenView);
            }
            set
            {
                // JR - getting a NullReferenceException for !fullScreenView.Equals(newValue)
                // So I tested for null first.  This seemed to solve the problem.
                // TJ - consolidates duplicate source code with short-circuited if statement
                if (fullScreenView == null || !fullScreenView.Equals(value))
                {
                    fullScreenView = (value) ? "1" : "0";
                    Set(fullScreenViewSettingName, fullScreenView);
                }
            }
        }

        public static int MaxItemCount
        {
            get
            {
                if (maxItemCount == -1)
                    maxItemCount = Convert.ToInt32(Get(maxItemCountSettingName));
                return maxItemCount;
            }
            set
            {
                if (maxItemCount != value)
                {
                    // update cached setting
                    maxItemCount = value;
                    // update in database
                    Set(maxItemCountSettingName, value.ToString());
                }
            }
        }

        public static int DownloadInterval
        {
            get
            {
                if (downloadInterval == -1)
                    downloadInterval = Convert.ToInt32(Get(downloadIntervalSettingName));
                return downloadInterval;
            }
            set
            {
                if (downloadInterval != value)
                {
                    // update cached setting
                    downloadInterval = value;
                    // update in database
                    Set(downloadIntervalSettingName, value.ToString());
                }
            }
        }

    }
}
